//
//  ZZCCollectionViewController.m
//  B段项目
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "ZZCCollectionViewController.h"
#import "HLJDetailViewController.h"
#import "ZZCMusicPlayerViewController.h"
#import "ZZCSettingViewCell.h"
#import "HLJHomeModel.h"
@interface ZZCCollectionViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,retain)UITableView *myCollectV;
@property(nonatomic,retain)NSMutableArray *array;
@property(nonatomic,assign)CGFloat cellHeight;

@end

@implementation ZZCCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.titleLabel.text = @"收藏";
    
    //图标
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - WIDTH/4.5, WIDTH/40, WIDTH/16, WIDTH/16)];
    img.image = [UIImage imageNamed:@"iconfont-xingxing.png"];
    img.center = CGPointMake(WIDTH - WIDTH/5, self.navigationController.navigationBar.frame.size.height /2);
    [self.rightView addSubview:img];
    
    self.array = [NSMutableArray array];
    
    self.myCollectV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT )];
    self.myCollectV.delegate = self;
    self.myCollectV.dataSource = self;
    [self.view addSubview:self.myCollectV];
    
    self.array = [[ZZCCollectionData collectDataBase]selectCollectList];
    
    [self.myCollectV registerClass:[ZZCSettingViewCell class] forCellReuseIdentifier:@"myCollectCell"];
}

#pragma mark - 行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.array.count;
}


#pragma mark - cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *myCell = @"myCollectCell";
    ZZCSettingViewCell *myCollectCell = [tableView dequeueReusableCellWithIdentifier:myCell];
    
    
    HLJHomeModel *homeM = [self.array objectAtIndex:indexPath.row];
    myCollectCell.myModel = homeM;
    
    self.cellHeight = [ZZCSettingViewCell heightForTitle:myCollectCell.myModel.title] + myCollectCell.titleLb.frame.origin.y + myCollectCell.imgView.frame.size.height + myCollectCell.type.frame.origin.y * 2;
    
    return myCollectCell;
}



#pragma mark - 行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return self.cellHeight;
}


#pragma mark - 触摸cell是触发的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HLJHomeModel *homeModel = [self.array objectAtIndex:indexPath.row];
    
    if ([homeModel.name isEqualToString:@"Ting"]) {
        
        [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"HomePush"];
        [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"CollectPush"];
        
        [ZZCMusicPlayerViewController shareMusicPlayerViewController].ting_id = homeModel._id;
        [self.navigationController pushViewController:[ZZCMusicPlayerViewController shareMusicPlayerViewController] animated:YES];
    }else{
        HLJDetailViewController *detailVC = [[HLJDetailViewController alloc]init];
        detailVC.detaId = homeModel._id;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
    
}


#pragma mark -- 每行的编辑样式(删除)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete;
}


#pragma mark - 删除编辑逻辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //如果是进行删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //先删除表中的数据 后删除cell
        HLJHomeModel *model = [self.array objectAtIndex:indexPath.row];
        //删除表中的数据
        [[ZZCCollectionData collectDataBase]deleteMyCollectData:model.title];
        
        
        [self.array removeObjectAtIndex:indexPath.row];
        //删除cell
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
    }
    
}

    
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
